package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.ActivityVo;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.OrderVo;
import com.ymatou.iapi.trading.operate.parameter.SellerCouponVo;
import com.ymatou.iapi.trading.operate.parameter.YmatouCouponVo;
import com.ymatou.iapi.trading.operate.parameter.enums.ActivityPromotionTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.AppTerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.PriceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.ProductRefundChannelEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SalesTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SellerCouponApplianceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.YmatouCouponApplianceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.PlaceOrderCall;
import com.ymatou.operate.trading.api.parameter.ApplySalesRefundRequestBean;
import com.ymatou.operate.trading.api.parameter.SalesRefundInfoBean;
import com.ymatou.operate.trading.api.service.ApplySalesRefundRequestCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.database.model.YmtRefundBill;
import com.ymttest.database.model.YmtRefundProduct;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtRefundWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 申请退货退款接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ApplySalesRefundRequest {
    private static ApplySalesRefundRequestBean applysalesrefundrequestBean;
    private static ApplySalesRefundRequestCall applysalesrefundrequestCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("申请退货退款接口");
    }

    @Before
    public void caseUp() {
        applysalesrefundrequestBean = new ApplySalesRefundRequestBean();
        applysalesrefundrequestCall = new ApplySalesRefundRequestCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(applysalesrefundrequestCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    // 一个订单下面多个商品
    public Map placeOrder() {
        Map map = new HashMap();
        try {
            PlaceOrderReq placeorderBean = new PlaceOrderReq();
            PlaceOrderCall placeorderCall = new PlaceOrderCall();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            // 创建产品
            Logger.comment("前置数据准备");
            HashMap<String, Object> productInfo = ProductCallService
                    .createProduct(sellerId, 3);
            Logger.comment("前置数据准备结束");
            String productId = (String) productInfo.get("productId");
            Logger.debug("productId:" + productId);
            String productName = (String) productInfo.get("productName");
            String pic = (String) productInfo.get("pic");
            BigDecimal price = (BigDecimal) (productInfo.get("price"));
            String catalogId = (String) productInfo.get("catalogId");
            YmatouCouponVo ymtCoupon = new YmatouCouponVo();
            // 平台优惠券
            BigDecimal couponValue = new BigDecimal(13);
            String couponCode = new OrderCallService().receivePlatformCoupon(userId, couponValue);
            ymtCoupon.setApplianceType(YmatouCouponApplianceTypeEnum.ALL);
            ymtCoupon.setCouponCode(couponCode);
            ymtCoupon.setCouponValue(couponValue);
            placeorderBean.setAppId("AutoTest");
            placeorderBean.setYmatouCoupon(ymtCoupon);
            placeorderBean.setUserId(userId);
            placeorderBean.setRequestId(TestDataManager.getRandomUUID()
                    .toString());
            placeorderBean.setTerminalSource(TerminalSourceEnum.IOS);
            placeorderBean.setAppTerminalSource(AppTerminalSourceEnum.Phone);
            placeorderBean.setSourceIp("127.0.0.1");
            placeorderBean.setAppId("ios");
            placeorderBean.setDeviceId("7777777777888888888888999");
            OrderGroupVo groupVo1 = new OrderGroupVo();
            placeorderBean.setOrderSource(OrderSourceEnum.PC);
            // 商家优惠券
            BigDecimal sellerCouponValue = new BigDecimal(15);
            String sellerCouponCode = new CouponCallService().receiveCouponInfo(sellerId, userId,
                    couponValue);
            SellerCouponVo sellerCoupon = new SellerCouponVo();
            sellerCoupon.setApplianceType(SellerCouponApplianceTypeEnum.ALL);
            sellerCoupon.setCouponCode(sellerCouponCode);
            sellerCoupon.setCouponValue(sellerCouponValue);
            groupVo1.setSellerCoupon(sellerCoupon);
            groupVo1.setSellerId(sellerId);
            List<OrderVo> orders = new ArrayList<OrderVo>();
            OrderVo ordervo1 = new OrderVo();
            ordervo1.setAutoCancelOrderHours(new BigDecimal(1));
            ordervo1.setOrderType(OrderTypeEnum.Spot);
            ordervo1.setPaidFull(true);
            ordervo1.setAddress("上海市闸北区灵石路636号");
            ordervo1.setLeaveWord("ymt autotest");
            ordervo1.setPhone("13100000001");
            ordervo1.setPostCode("200001");
            ordervo1.setTelephone("021-51002100");
            ordervo1.setReceiveName("李四");
            List<OrderItemVo> items = new ArrayList<OrderItemVo>();
            OrderItemVo orderitem1 = new OrderItemVo();
            orderitem1.setCatalogId(TestDataManager.getRandomUUID().toString());
            orderitem1.setProductNum(1);
            orderitem1.setProductName(productName);
            orderitem1.setPriceType(PriceTypeEnum.VipPrice);
            orderitem1.setProductId(TestDataManager.getRandomUUID().toString());
            price = new BigDecimal(210);
            orderitem1.setEarnestPrice(price);
            orderitem1.setProductPrice(price);
            orderitem1.setOriginalPrice(price);
            orderitem1.setSalesType(SalesTypeEnum.AlreadyInStock);
            orderitem1.setLogisticsType(LogisticsTypeEnum.SailProtect);
            orderitem1.setProductRefundChannel(ProductRefundChannelEnum.Official);
            orderitem1.setBrandId(5);
            orderitem1.setPackageNo("11111111");
            orderitem1
                    .setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
            orderitem1.setProductRecordNo("1234567");
            orderitem1.setProductRefundChannel(ProductRefundChannelEnum.Official);
            orderitem1
                    .setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
            ActivityVo activity = new ActivityVo();
            activity.setActivityId(46870);
            activity.setActivityBeginDate(YMTDateUtil.getBeforeOrNextDay(-10, "yyyy-MM-dd'T'HH:mm:ss"));
            activity.setActivityEndDate(YMTDateUtil.getBeforeOrNextDay(10, "yyyy-MM-dd'T'HH:mm:ss"));
            activity.setActivityTemplateId(12345678);
            activity.setCommissionRate(new BigDecimal(1.1));
            activity.setCost(true);
            activity.setFeeFree(false);
            activity.setPremium(new BigDecimal(1.5));
            activity.setPromotionPrice(new BigDecimal(15));
            activity.setPromotionType(ActivityPromotionTypeEnum.DiscountRate);
            activity.setUseCoupon(true);
            orderitem1.setActivity(activity);
            ordervo1.setOrderItems(items);
            ordervo1.setFreight(new BigDecimal("21"));
            // 第2件商品
            OrderItemVo orderitem2 = new OrderItemVo();
            orderitem2.setCatalogId(TestDataManager.getRandomUUID().toString());
            orderitem2.setProductNum(1);
            orderitem2.setProductName(productName);
            orderitem2.setPriceType(PriceTypeEnum.VipPrice);
            orderitem2.setProductId(TestDataManager.getRandomUUID().toString());
            price = new BigDecimal(270);
            orderitem2.setEarnestPrice(price);
            orderitem2.setProductPrice(price);
            orderitem2.setOriginalPrice(price);
            orderitem2.setSalesType(SalesTypeEnum.AlreadyInStock);
            orderitem2.setLogisticsType(LogisticsTypeEnum.SailProtect);
            orderitem2.setProductRefundChannel(ProductRefundChannelEnum.Official);
            orderitem2.setBrandId(5);
            orderitem2.setPackageNo("11111111");
            orderitem2
                    .setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
            orderitem2.setProductRecordNo("1234567");
            orderitem2.setProductRefundChannel(ProductRefundChannelEnum.Official);
            orderitem2
                    .setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
            orderitem2.setActivity(activity);
            items.add(orderitem1);
            items.add(orderitem2);
            ordervo1.setOrderItems(items);
            ordervo1.setFreight(new BigDecimal("21"));
            orders.add(ordervo1);
            groupVo1.setOrders(orders);
            placeorderBean.setOrderGroups(Arrays.asList(groupVo1));
            placeorderCall.setData(false, placeorderBean);
            placeorderCall.callService();
            JSONArray orderIds = placeorderCall.getOrderIds();
            int tradingId = placeorderCall.getTradingId();
            map.put("orderIds", orderIds);
            map.put("tradingId", tradingId);
        } catch (Exception e) {
            Logger.fail(e);
        }
        return map;
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_ApplySalesRefundRequest_001() {
        Logger.start(true, "申请退货退款成功");
        try {
            Map map = placeOrder();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            JSONArray orderIds = (JSONArray) map.get("orderIds");
            int tradingId = (Integer) map.get("tradingId");
            int orderId = orderIds.getInt(0);
            applysalesrefundrequestBean.setOrderId(orderId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = new DefaultWapper().selectOrderInfoByOderId(orderId);
            List<SalesRefundInfoBean> salesRefundInfoList = new ArrayList<SalesRefundInfoBean>();
            BigDecimal totalPrice = new BigDecimal(0);
            for (int i = 0; i < ymtOrdersInfoList.size(); i++) {
                String catalogId = (String) ymtOrdersInfoList.get(i).get("sCatalogId");
                String productId = (String) ymtOrdersInfoList.get(i).get("sProductId");
                // 如果規格為null,給個默認值
                int productNum = (Integer) ymtOrdersInfoList.get(i).get("iAmount");
                // 计算订单总价格 商品价格 X 数量+运费
                BigDecimal refoundAmout = ((BigDecimal) ymtOrdersInfoList.get(i).get("fProductPrice"))
                        .multiply(new BigDecimal(productNum));
                SalesRefundInfoBean salesRefundInfo = new SalesRefundInfoBean();
                salesRefundInfo.setCatalogId(catalogId);
                // productid 丢在resaon 里，校验数据用
                salesRefundInfo.setReason(productId);
                salesRefundInfo.setRefundAmount(refoundAmout);
                salesRefundInfo.setRefundBillNo("YMTTk" + i + orderId);
                salesRefundInfo.setRefundProductNum(10);
                salesRefundInfo.setRefundType(1);
                salesRefundInfoList.add(salesRefundInfo);
            }
            applysalesrefundrequestBean.setSalesRefundInfoList(salesRefundInfoList);
            applysalesrefundrequestCall.setData(applysalesrefundrequestBean);
            applysalesrefundrequestCall.callService();
            Logger.verifyEquals("ok", applysalesrefundrequestCall.getReturnData(), "验证申请退货退款接口返回正常");
            if (applysalesrefundrequestCall.getReturnData().equals("ok")) {
                // 数据库验证
                YmtRefundWapper ymtRefund = new YmtRefundWapper();
                for (SalesRefundInfoBean refund : applysalesrefundrequestBean.getSalesRefundInfoList()) {
                    String refundBillNo = refund.getRefundBillNo();
                    Map m = new HashMap();
                    m.put("orderId", orderId);
                    m.put("refundBillNo", refundBillNo);
                    YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
                    YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m).get(0);
                    Logger.comment("校验Ymt_RefundBill表");
                    Logger.verifyEquals(orderId, refundBill.getOrderId(), "验证OrderId");
                    Logger.verifyEquals(refundBillNo, refundBill.getRefundBillNo(), "验证RefundBillNo");
                    Logger.verifyEquals(refund.getRefundAmount(), refundBill.getRefundAmount(), "验证RefundAmount");
                    Logger.verifyEquals(0, refundBill.getRefundStatus(), "验证RefundStatus");
                    Logger.verifyEquals(refund.getRefundAmount(), refundBill.getSettlementAmount(),
                            "验证SettlementAmount");
                    Logger.verifyEquals(1, refundBill.getSalesRefundStatus(), "验证SettlementStatus");
                    Logger.verifyEquals(refund.getRefundType(), refundBill.getRefundType(), "验证RefundType");
                    Logger.verifyEquals(refund.getReason(), refundBill.getReason(), "验证Reason");
                    Logger.verifyEquals(sellerId, refundBill.getSellerId(), "验证SellerId");
                    Logger.verifyEquals(userId, refundBill.getBuyerId(), "验证BuyerId");
                    Logger.verifyEquals(orderId, refundBill.getOrderId(), "验证OrderId");
                    Logger.comment("校验Ymt_RefundProduct表");
                    Logger.verifyEquals(orderId, refundPro.getOrderId(), "验证OrderId");
                    Logger.verifyEquals(refundBillNo, refundPro.getRefundBillNo(), "验证RefundBillNo");
                    Logger.verifyEquals(refund.getReason(), refundPro.getProductId(), "验证ProductId");
                    Logger.verifyEquals(refund.getCatalogId(), refundPro.getCatalogId(), "验证CatalogId");
                    Logger.verifyEquals(refund.getRefundProductNum(), refundPro.getRefundProductNum(),
                            "验证RefundProductNum");
                }
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_ApplySalesRefundRequest_002() {
        Logger.start(false, "申请退货退款,订单号不存在");
        try {
            Map map = placeOrder();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            JSONArray orderIds = (JSONArray) map.get("orderIds");
            int tradingId = (Integer) map.get("tradingId");
            int orderId = orderIds.getInt(0);
            applysalesrefundrequestBean.setOrderId(11111);
            List<SalesRefundInfoBean> salesRefundInfoList = new ArrayList<SalesRefundInfoBean>();
            SalesRefundInfoBean salesRefundInfo = new SalesRefundInfoBean();
            salesRefundInfo.setCatalogId("ssss");
            salesRefundInfo.setReason("autotest");
            salesRefundInfo.setRefundAmount(new BigDecimal(100));
            salesRefundInfo.setRefundBillNo("YMTTF" + orderId);
            salesRefundInfo.setRefundProductNum(10);
            salesRefundInfo.setRefundType(1);
            salesRefundInfoList.add(salesRefundInfo);
            applysalesrefundrequestBean.setSalesRefundInfoList(salesRefundInfoList);
            applysalesrefundrequestCall.setData(applysalesrefundrequestBean);
            applysalesrefundrequestCall.callService();
            Logger.verifyEquals("ok", applysalesrefundrequestCall.getReturnData(), "验证申请退货退款接口返回正常");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }


    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_ApplySalesRefundRequest_003() {
        Logger.start(true, "发货后,申请退货退款成功");
        try {
            Map map = placeOrder();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            JSONArray orderIds = (JSONArray) map.get("orderIds");
            int tradingId = (Integer) map.get("tradingId");
            int orderId = orderIds.getInt(0);
            applysalesrefundrequestBean.setOrderId(orderId);
            OrderCallService orderCallService = new OrderCallService();
            // 应付款全金额
            BigDecimal amount = orderCallService.getNeedPrePayAmountOfCash(orderId);
            // 付款
            orderCallService.orderPayRequest(userId, amount, tradingId);
            // 卖家接单
            orderCallService.acceptOrder(orderId, sellerId);
            // 发货
            orderCallService.dispatched(orderId, sellerId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = new DefaultWapper().selectOrderInfoByOderId(orderId);
            List<SalesRefundInfoBean> salesRefundInfoList = new ArrayList<SalesRefundInfoBean>();
            BigDecimal totalPrice = new BigDecimal(0);
            for (int i = 0; i < ymtOrdersInfoList.size(); i++) {
                String catalogId = (String) ymtOrdersInfoList.get(i).get("sCatalogId");
                String productId = (String) ymtOrdersInfoList.get(i).get("sProductId");
                // 如果規格為null,給個默認值
                int productNum = (Integer) ymtOrdersInfoList.get(i).get("iAmount");
                // 计算订单总价格 商品价格 X 数量+运费
                BigDecimal refoundAmout = ((BigDecimal) ymtOrdersInfoList.get(i).get("fProductPrice"))
                        .multiply(new BigDecimal(productNum));
                SalesRefundInfoBean salesRefundInfo = new SalesRefundInfoBean();
                salesRefundInfo.setCatalogId(catalogId);
                // productid 丢在resaon 里，校验数据用
                salesRefundInfo.setReason(productId);
                salesRefundInfo.setRefundAmount(refoundAmout);
                salesRefundInfo.setRefundBillNo("YMTTk" + i + orderId);
                salesRefundInfo.setRefundProductNum(10);
                salesRefundInfo.setRefundType(1);
                salesRefundInfoList.add(salesRefundInfo);
            }
            applysalesrefundrequestBean.setSalesRefundInfoList(salesRefundInfoList);
            applysalesrefundrequestCall.setData(applysalesrefundrequestBean);
            applysalesrefundrequestCall.callService();
            Logger.verifyEquals("ok", applysalesrefundrequestCall.getReturnData(), "验证申请退货退款接口返回正常");
            if (applysalesrefundrequestCall.getReturnData().equals("ok")) {
                // 数据库验证
                YmtRefundWapper ymtRefund = new YmtRefundWapper();
                for (SalesRefundInfoBean refund : applysalesrefundrequestBean.getSalesRefundInfoList()) {
                    String refundBillNo = refund.getRefundBillNo();
                    Map m = new HashMap();
                    m.put("orderId", orderId);
                    m.put("refundBillNo", refundBillNo);
                    YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
                    YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m).get(0);
                    Logger.comment("校验Ymt_RefundBill表");
                    Logger.verifyEquals(orderId, refundBill.getOrderId(), "验证OrderId");
                    Logger.verifyEquals(refundBillNo, refundBill.getRefundBillNo(), "验证RefundBillNo");
                    Logger.verifyEquals(refund.getRefundAmount(), refundBill.getRefundAmount(), "验证RefundAmount");
                    Logger.verifyEquals(0, refundBill.getRefundStatus(), "验证RefundStatus");
                    Logger.verifyEquals(refund.getRefundAmount(), refundBill.getSettlementAmount(),
                            "验证SettlementAmount");
                    Logger.verifyEquals(1, refundBill.getSalesRefundStatus(), "验证SettlementStatus");
                    Logger.verifyEquals(refund.getRefundType(), refundBill.getRefundType(), "验证RefundType");
                    Logger.verifyEquals(refund.getReason(), refundBill.getReason(), "验证Reason");
                    Logger.verifyEquals(sellerId, refundBill.getSellerId(), "验证SellerId");
                    Logger.verifyEquals(userId, refundBill.getBuyerId(), "验证BuyerId");
                    Logger.verifyEquals(orderId, refundBill.getOrderId(), "验证OrderId");
                    Logger.comment("校验Ymt_RefundProduct表");
                    Logger.verifyEquals(orderId, refundPro.getOrderId(), "验证OrderId");
                    Logger.verifyEquals(refundBillNo, refundPro.getRefundBillNo(), "验证RefundBillNo");
                    Logger.verifyEquals(refund.getReason(), refundPro.getProductId(), "验证ProductId");
                    Logger.verifyEquals(refund.getCatalogId(), refundPro.getCatalogId(), "验证CatalogId");
                    Logger.verifyEquals(refund.getRefundProductNum(), refundPro.getRefundProductNum(),
                            "验证RefundProductNum");
                }
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_ApplySalesRefundRequest_004() {
        Logger.start(true, "发货后,部分退款");
        try {
            Map map = placeOrder();
            int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
            JSONArray orderIds = (JSONArray) map.get("orderIds");
            int tradingId = (Integer) map.get("tradingId");
            int orderId = orderIds.getInt(0);
            applysalesrefundrequestBean.setOrderId(orderId);
            OrderCallService orderCallService = new OrderCallService();
            // 应付款全金额
            BigDecimal amount = orderCallService.getNeedPrePayAmountOfCash(orderId);
            // 付款
            orderCallService.orderPayRequest(userId, amount, tradingId);
            // 卖家接单
            orderCallService.acceptOrder(orderId, sellerId);
            // 发货
            orderCallService.dispatched(orderId, sellerId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = new DefaultWapper().selectOrderInfoByOderId(orderId);
            List<SalesRefundInfoBean> salesRefundInfoList = new ArrayList<SalesRefundInfoBean>();

            String catalogId = (String) ymtOrdersInfoList.get(0).get("sCatalogId");
            String productId = (String) ymtOrdersInfoList.get(0).get("sProductId");
            // 如果規格為null,給個默認值
            int productNum = (Integer) ymtOrdersInfoList.get(0).get("iAmount");

            BigDecimal refoundAmout = new BigDecimal(130);
            SalesRefundInfoBean salesRefundInfo = new SalesRefundInfoBean();
            salesRefundInfo.setCatalogId(catalogId);
            // productid 丢在resaon 里，校验数据用
            salesRefundInfo.setReason(productId);
            salesRefundInfo.setRefundAmount(refoundAmout);
            salesRefundInfo.setRefundBillNo("YMTTk" + 0 + orderId);
            salesRefundInfo.setRefundProductNum(10);
            salesRefundInfo.setRefundType(1);
            salesRefundInfoList.add(salesRefundInfo);

            applysalesrefundrequestBean.setSalesRefundInfoList(salesRefundInfoList);
            applysalesrefundrequestCall.setData(applysalesrefundrequestBean);
            applysalesrefundrequestCall.callService();

            Logger.verifyEquals("ok", applysalesrefundrequestCall.getReturnData(), "验证申请退货退款接口返回正常");
            if (applysalesrefundrequestCall.getReturnData().equals("ok")) {
                // 数据库验证
                YmtRefundWapper ymtRefund = new YmtRefundWapper();
                for (SalesRefundInfoBean refund : applysalesrefundrequestBean.getSalesRefundInfoList()) {
                    String refundBillNo = refund.getRefundBillNo();
                    Map m = new HashMap();
                    m.put("orderId", orderId);
                    m.put("refundBillNo", refundBillNo);
                    YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
                    YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m).get(0);
                    Logger.comment("校验Ymt_RefundBill表");
                    Logger.verifyEquals(orderId, refundBill.getOrderId(), "验证OrderId");
                    Logger.verifyEquals(refundBillNo, refundBill.getRefundBillNo(), "验证RefundBillNo");
                    Logger.verifyEquals(refund.getRefundAmount().setScale(2),
                            refundBill.getRefundAmount(), "验证RefundAmount");
                    Logger.verifyEquals(0, refundBill.getRefundStatus(), "验证RefundStatus");
                    Logger.verifyEquals(refund.getRefundAmount().setScale(2),
                            refundBill.getSettlementAmount(),
                            "验证SettlementAmount");
                    Logger.verifyEquals(1, refundBill.getSalesRefundStatus(), "验证SettlementStatus");
                    Logger.verifyEquals(refund.getRefundType(), refundBill.getRefundType(), "验证RefundType");
                    Logger.verifyEquals(refund.getReason(), refundBill.getReason(), "验证Reason");
                    Logger.verifyEquals(sellerId, refundBill.getSellerId(), "验证SellerId");
                    Logger.verifyEquals(userId, refundBill.getBuyerId(), "验证BuyerId");
                    Logger.verifyEquals(orderId, refundBill.getOrderId(), "验证OrderId");
                    Logger.comment("校验Ymt_RefundProduct表");
                    Logger.verifyEquals(orderId, refundPro.getOrderId(), "验证OrderId");
                    Logger.verifyEquals(refundBillNo, refundPro.getRefundBillNo(), "验证RefundBillNo");
                    Logger.verifyEquals(refund.getReason(), refundPro.getProductId(), "验证ProductId");
                    Logger.verifyEquals(refund.getCatalogId(), refundPro.getCatalogId(), "验证CatalogId");
                    Logger.verifyEquals(refund.getRefundProductNum(), refundPro.getRefundProductNum(),
                            "验证RefundProductNum");
                }
            }
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
